<?php

//include genotype.module;

function get_poolId_param()
{
  $pool = isset($_GET['pool']) ? $_GET['pool'] : '-1'; 
  return (int)$pool;
}

function genotype_table_display($offset = 0, $limit = 10)
{
	global $pager_page_array, $pager_total, $pager_total_items, $pool_id;

	$tbl_header = array();
	$tbl_header[] = array("data" => "Genotype Name, Author");
	$tbl_header[] = array("data" => "Size");
	$tbl_header[] = array("data" => "Action");
	$tbl_header[] = array("data" => "Genotype");
	
	
	$page = pager_find_page();
	$limit = 10;
	$offset = $limit * $page;
//	pager_default_initialize(100, 10);

	$query = db_select('genotypes', 'g');
	$query->join('users', 'u', 'u.uid = g.us_id');
	$query = $query->fields('g', array('id', 'name', 'count_p', 'count_j', 'count_n', 'count_c', 'geno_html_short', 'geno_f0', 'reviewed_by'))->fields('u', array('name'));
	$query_count = $query->countQuery();
	$pool_id = $poolId = get_poolId_param();
	if ($poolId > 0)
		$query = $query->condition('pool_id', $poolId);
	$query = $query->range($offset, $limit);

	if (!user_access('review genotype'))
	{
		$query->isNotNull("reviewed_by");
	}

	$count = $query_count->execute()->fetchAssoc();
	$count = $count['expression'];
	$genotypes = $query->execute();
    	
	$rows = array();
	while($genotype = $genotypes->fetchAssoc())
	{
	    $applet = "<applet archive=\"Favi.jar\" code=\"favi/Favi.class\" codebase=\"favi\" height=\"20\" width=\"60\" align=\"middle\">" .
    	            "<param name=\"button\" value=\"view\">" .
        	        "<param name=\"genotype\" value=\"" . $genotype['geno_f0'] . "\">" .
            	    "</applet>";

	  $rows[] = array(
	    array('data' => $genotype['name'] . ',<br> by ' .
				$genotype['u_name']),
	    array('data' => 'P: ' . $genotype['count_p'] . '<br>' . 
				'J: ' . $genotype['count_j'] . '<br>' .
				'N: ' . $genotype['count_n'] . '<br>' .
				'C: ' . $genotype['count_c']),
	    array('data' => (l("details", 'genotype_details/'.$genotype['id']) . "<br/>" .
				l("download", "genotype_download/". $genotype['id']). "<br/>" .
				$applet /*. "<br/>" . l("view", 'genotype_view/' . $genotype['id'])*/)),
	    array('data' => $genotype['geno_html_short'])
	  );

	}
	
	$pager_total_items[0] = $count;
	$pager_page_array[0] = $page;
	$pager_total[0] = ceil($count / $limit);

	$pager = theme_pager(array('tags' => array(), 'element' => 0, 'parameters' => array(), 'quantity' => $pager_total[0]));

	$variables = array(
	  "header" => $tbl_header, 
	  "rows" => $rows, 
	  "attributes" => array(), 
	  "caption" => "", 
	  "colgroups" => array(),
	  "sticky" => "",
	  "empty" => "");


	$form = drupal_get_form('filter_pool_form');
	$form = drupal_render($form);

	$html = theme_table($variables);
	return $form.$html.$pager;
}

function filter_pool_form($form, &$form_state)
{
    $query = db_select('gene_pool', 'g');
    $pools = $query
        ->fields('g', array('id', 'name'))
        ->execute();

    $pools_form = array(-1 => '-- All --');
	$poolId = get_poolId_param();
    while ($pool = $pools->fetchAssoc())
    {
        $pools_form[$pool['id']] = $pool['name'];
    }   

    $form['pool_id'] = array(
      '#type' => 'select',
      '#title' => t('Pool'),
      '#options' => $pools_form,
      '#default_value' => $poolId
    );

    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Filter'),
    );

    $form['submit2'] = array(
      '#type' => 'button',
      '#value' => t('Download pool'),
      '#executes_submit_callback' => TRUE,
      '#submit' => array('pool_download_clicked'),
    );

    return $form;
}

function filter_pool_form_submit($form, &$form_state)
{
	$vals = $form_state['values'];
	global $pool_id;
	$pool_id = $vals['pool_id'];
	drupal_goto('genotype_table', array('query' => array('pool' => $vals['pool_id'])));
}

function pool_download_clicked($form, &$form_state)
{

	global $pool_id;
	drupal_goto('pool_download/'.$pool_id);

}

?>
